import 'package:camera/camera.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/route_manager.dart';
import 'package:get_storage/get_storage.dart';
import 'package:overload_guard/app_theme.dart';
import 'package:overload_guard/configs.dart';
import 'package:overload_guard/main_provider.dart';
import 'package:overload_guard/pages/data_input_page.dart';
import 'package:overload_guard/pages/data_list_page.dart';
import 'package:overload_guard/pages/sign_in_page.dart';
import 'package:overload_guard/pages/user_center_page.dart';
import 'package:overload_guard/services/db_service.dart';

final dio = Dio();
late List<CameraDescription> cameras;

void main() async{
  await _appInit();

  WidgetsFlutterBinding.ensureInitialized();

  cameras = await availableCameras();

  runApp(const MyApp());
}

Future _appInit() async{
  // 配置全局 Controller
  Get.put(GlobalController(appConfig: DevConfig()));

  await GetStorage.init();

}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: '超偏载检测系统',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.black),
        useMaterial3: true,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  @override
  void initState() {
    _checkSignIn();
    super.initState();
  }

  _checkSignIn() async{

    // 判断当前是否登陆
    print('当前登陆状态:${(await DBService.getData(DBKey.userSignInStatus))}');
    bool status = (await DBService.getData(DBKey.userSignInStatus)) == true;
    if(!status) {
      Get.offAll(SignInPage());
    }
  }

  @override
  Widget build(BuildContext context) {

    Widget mainButton(String text, Function()? onPressed) {
      return ElevatedButton(
        onPressed: onPressed,
        child: Text(
          text,
          style: TextStyle(color: Colors.white, fontSize: Theme.of(context).textTheme.titleMedium!.fontSize),
        ),
        style: ButtonStyle(
            minimumSize: MaterialStateProperty.all(Size(200, 55)),
            backgroundColor: MaterialStateProperty.all(AppTheme.primaryColor), shape: MaterialStateProperty.all(RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(10.0), // 设置圆角弧度
        ))),
      );
    }

    return Scaffold(
      appBar: AppBar(
        backgroundColor: AppTheme.primaryColor,
        title: Text(
          '超偏载检测系统',
          style: TextStyle(color: Colors.white),
        ),
        centerTitle: true,
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: <Widget>[
            mainButton("车辆信息录入", () {
              Get.to(DataInputPage());
            }),
            mainButton("超偏载检测", () {Get.to(DataListPage());}),
            mainButton("个人中心", () {Get.to(UserCenterPage());}),
          ],
        ),
      ),
    );
  }
}
